define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recharge/index' + location.search,
                   // add_url: 'recharge/add',
                    //edit_url: 'recharge/edit',
                    //del_url: 'recharge/del',
                    multi_url: 'recharge/multi',
                    import_url: 'recharge/import',
                    export_url: 'recharge/export',
                    table: 'recharge',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#score_sum").text(data.extend.score_sum);
                $("#fixed_score_sum").text(data.extend.fixed_score_sum);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.mobile', title: __('Phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'merchant.name', title: __('充值商家'), operate: 'LIKE'},
                        {field: 'score', title: __('Score')},
                        {field: 'fixed_score', title: __('Fixed_score')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'course.name', title: __('Course_id'), visible: false},
                        {field: 'course_name', title: __('Course_id'), operate: false},
                        /*{field: 'card.name', title: __('Card_id')},*/
                        {field: 'recharge.createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'status', title: __('Status'), searchList: {"0":__('未审核'),"1":__('审核通过'),"2":__('审核驳回')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: __('Type'), searchList: {"1":__('充值积分'),"2":__('充值卡')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'click',
                                    title: __('审核'),
                                    text: __('审核'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'recharge/shenhe',
                                    //visible属性可以让按钮显示或者隐藏，这里通过判断status = 6就显示，否则就隐藏
                                    visible:function(row){
                                        console.log(row);
                                        if(row.status == 0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        shenhe: function () {

            Controller.api.bindevent();
            // Controller.api.shenhesave();
            //取消，支付
            $(document).on('click', '.btn-status-click', function () {
                let order = $(this).data('order');
               let status = order.status;
               let id = order.id;
                $.post('recharge/shenhe', {
                    id,
                    status,
                    ...order
                }, function (res) {
                    if (res.code == 1) {
                        parent.Toastr.success(res.msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.layer.closeAll();

                    } else {
                        parent.Toastr.error(res.msg);
                    }
                });
            });
            //驳回
            $(document).on('click', '.btn-statusre-click', function () {
                //let id = $(this).data('id');
                /* var index = parent.layer.getFrameIndex(window.name);
                 parent.layer.close(index);
                 parent.layer.closeAll("refresh");*/
                let id = $("#c-id").val();
                // alert(id);
                parent.Fast.api.open('work/shenhere?ids='+id, __('驳回工单'),{
                    //接收传过来的参数
                    callback:function(value){
                        //在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
                    }
                });
            });
            $(document).on('click', '.btn-statusrere-click', function () {
                parent.layer.closeAll();
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
